package cn.yunhe.servlet;

import cn.yunhe.bean.Page;
import cn.yunhe.bean.Travel;
import cn.yunhe.biz.TravelService;
import cn.yunhe.util.PageUtil2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/ToTravelServlet")
public class ToTravelServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到当前页
        String currPage=request.getParameter("pageIndex");
        if(currPage==null){
            currPage="1";
        }
        int pageIndex=Integer.parseInt(currPage);
        //得到分页的List集合
        TravelService travelService=new TravelService();

       /* if(travelList!=null && travelList.size()>0){
        }*/
        int travelCount=travelService.getTravelCount();
        //总页数
        int totalPages=PageUtil2.getTotalPages(travelCount,PageUtil2.travelPageSize);

        //首尾页的判断
        if(pageIndex>totalPages){
            pageIndex=totalPages;
        }
        if(pageIndex<1){
            pageIndex=1;
        }
        //根据当前的数据赋值给Page
        Page page=new Page();
        page.setStartPage(PageUtil2.travelPageSize * (pageIndex-1));
        page.setEndPage(PageUtil2.travelPageSize);

        //List<Travel> travelList1=travelService.getTravelByPages(page);
        List<Travel> travelList=travelService.getTravelByPages(page);
       // request.setAttribute("travelList1",travelList1);
        request.setAttribute("pageIndex", pageIndex);
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("travelList", travelList);

        //跳转页面
        /*response.sendRedirect("travelNote.jsp");*/
        request.getRequestDispatcher("travelNote.jsp").forward(request,response);

    }



}
